<?php
$dirList = explode ( '/', $_SERVER ['PHP_SELF'] );
$document_root = $_SERVER ["DOCUMENT_ROOT"];
require "$document_root/$dirList[1]/conf/conf.php";

$connMng = new ConnManger ( $db_conf, $mem_conf );
$connStep = $connMng->getConn_step ();

$step_mem_list = Step_count_DAO::getList($connStep);


$mode = $_POST["mode"];
$result = "normal";
$needle = $_GET['needle'];
if ($mode == "input") {
        $step_mem_day_list = Step_count_DAO::getListByOrderDay($connStep);

        //按昨日步数排名
        foreach ($step_mem_day_list as $step_mem_record) {
                $id = $step_mem_record->getId();
                $inputStep = $_POST["stepday$id"];
                $step_mem_record->setStepyestoday($inputStep);
                $oldTodayStep = $step_mem_record->getStepday();
                $step_mem_record->setStepday($inputStep);
                $step_mem_record->setStepall($step_mem_record->getStepall() + ($inputStep - $oldTodayStep));
                $oldStepTop = $step_mem_record->getStepTopPre();
                
                if ( $inputStep>$oldStepTop ) {
                        $stepTop = $inputStep;
                        $step_mem_record->setStepTop($stepTop);
                }elseif ($inputStep<$oldStepTop) {
                        $stepTop = $oldStepTop;
                        $step_mem_record->setStepTop($stepTop);
                }

                Step_count_DAO::update($step_mem_record,$connStep);

        }

        $step_mem_day_list = Step_count_DAO::getList($connStep);
        foreach ($step_mem_day_list as $step_mem_day_record) {
                $stepDay = $step_mem_day_record->getStepday();
                //按昨日步数排名算积分
                if($stepDay>=40000){
                        $achievday = 6;
                }
                if($stepDay>=30000&&$stepDay<40000){
                        $achievday = 5;
                }
                if($stepDay>=25000&&$stepDay<30000){
                        $achievday = 4;
                }
                if($stepDay>=20000&&$stepDay<25000){
                        $achievday = 3;
                }
                if($stepDay>=15000&&$stepDay<20000){
                        $achievday = 2;
                }
                if($stepDay>=10000&&$stepDay<15000) {
                        $achievday = 1;
                }
                if($stepDay<10000) {
                        $achievday = 0;
                }
                $oldachievday = $step_mem_day_record->getAchievementday();
                $step_mem_day_record->setAchievementday($achievday);
                $step_mem_day_record->setAchievementall($step_mem_day_record->getAchievementall() + ($achievday-$oldachievday));
                Step_count_DAO::update($step_mem_day_record,$connStep);
        }
      /*  $positionDay = 1;
        $step_mem_day_list = Step_count_DAO::getListByOrderDay($connStep);
        foreach ($step_mem_day_list as $step_mem_record) {

                //按昨日步数排名算积分
                if($positionDay>=1&&$positionDay<=8){
                        $achievday = 7;
                }
                if($positionDay>=9&&$positionDay<=20){
                        $achievday = 6;
                }
                if($positionDay>=21&&$positionDay<=40){
                        $achievday = 5;
                }
                if($positionDay>=41&&$positionDay<=60){
                        $achievday = 4;
                }
                if($positionDay>=61&&$positionDay<=80){
                        $achievday = 3;
                }
                if($positionDay>=81&&$positionDay<=100){
                        $achievday = 2;
                }
                if($positionDay>=101) {
                        $achievday = 1;
                }
                $step_mem_record->setPositionDay($positionDay);
                $positionDay++;

                $oldachievday = $step_mem_record->getAchievementday();
                $step_mem_record->setAchievementday($achievday);
                $step_mem_record->setAchievementall($step_mem_record->getAchievementall() + ($achievday-$oldachievday));
                Step_count_DAO::update($step_mem_record,$connStep);
        }*/

        //按总积分排名
        $step_mem_achieve_list = Step_count_DAO::getListByOrderAchieve($connStep);
        $positionAll = 1;
        foreach($step_mem_achieve_list as $step_mem_achieve_list_record ) {
                $step_mem_achieve_list_record->setPositionAll($positionAll);
                Step_count_DAO::update($step_mem_achieve_list_record,$connStep);
                $positionAll++;
        }

        $step_mem_list = Step_count_DAO::getList($connStep);
        $result = "succ";
}

$step_mem_list = Step_count_DAO::getListByNeedle($needle,$connStep);
if (count ( $step_mem_list ) == 0) {
        $result = "notfound";
}

?>

<?php
$page_groupId = GROUP_STEP_MNG;
$page_pageId = PAGE_STEP_INPUT;
require "$document_root/$dirList[1]/mng/view/framepages/moudle_base.php";
?>


<div class="main-content">
        <div class="search-well">
                <form class="form-inline" style="margin-top: 0px;">
                        用户检索 &nbsp;<input class="input-xlarge form-control"
                                          placeholder="用户昵称/备注" id="4sStoreName" type="text"> &nbsp;
                        <button class="btn btn-default" type="button" id="searchButton">
                                <i class="fa fa-search"></i> 查找
                        </button>
                </form>
        </div>

        <form method="post" action="" onSubmit="javascript:return window.confirm('确认提交吗？')" >

                <table class="table" style="word-break:break-all">
                        <thead>
                        <tr>

                                <th>ID</th>
                                <th>头像</th>
                                <th width="9%">昵称</th>
                                <th>备注</th>
                                <th width="10%">当前总积分排名</th>
                                <th>总积分</th>
                                <th>今日积分</th>
                                <th>总步数</th>
                                <th>昨日步数</th>
                                <th>请录入今日步数</th>
                        </tr>
                        </thead>

                        <tbody>
                        <?php

                        foreach($step_mem_list as $step_mem_record) {
                                $id = $step_mem_record->getId();
                                $img = $step_mem_record->getImg();
                                $name = $step_mem_record->getName();
                                $markName = $step_mem_record->getMarkName();
                                $achievall = $step_mem_record->getAchievementall();
                                $positionall = $step_mem_record->getPositionAll();
                                $achievdayy = $step_mem_record->getAchievementday();
                                $totalCount = $step_mem_record->getStepall();
                                $yestodayCount = $step_mem_record->getStepyestoday();
                                $dayCount = $step_mem_record->getStepday();
                                echo "
                                <tr>
                                <td>$id</td>    
                                <td><img src='$img' width='35' height='35' /></td>
                                <td>$name</td>
                                <td>$markName</td>
                                <td>$positionall</td>
                                <td>$achievall</td> 
                                <td>$achievdayy</td>
                                <td>$totalCount</td>
                                <td>$yestodayCount</td>
                                <td><input type = 'text' name = 'stepday$id' value='$dayCount' /></td>
                                </tr>
                                ";
                        }
                        ?>

                        </tbody>
                </table>

                <input type="submit" class='btn btn-danger' value='提交'>
                <input type="hidden" name="mode" id="mode" value="input">
        </form>

        <?php
        require "$document_root/$dirList[1]/mng/view/framepages/footer.php";
        ?>
</div>
<script src="../lib/bootstrap/js/bootstrap.js"></script>

<script type="text/javascript">

        needle = "<?php echo $needle?>";
        $('#searchButton').click(function(){
                needle = $('#4sStoreName').val();
                window.location.href = "?needle="+needle;
        });
</script>

<script>

        function init() {
                var msg = "<?php echo $result ?>";
                if (msg == 'succ') {
                        alert("提交成功");
                }
                if (msg == 'notfound') {
                        alert("未检测到相关用户");
                }
        }

</script>
</body>
</html>
